package com.android.commcount.ui.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.android.baselibrary.tool.Log;
import com.android.commcount.R;
import com.android.commcount.bean.Count_DetailInfo;
import com.android.commcount.event.Event_Count_ChangeCircleRadius;
import com.android.commcount.manager.ImageRecConfig;

import org.greenrobot.eventbus.EventBus;

/**
 * 精度调整
 */
public class CommCount_AdjustView extends LinearLayout implements View.OnClickListener {

    Context context;

    public CommCount_AdjustView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        initView(attrs);
    }

    public CommCount_AdjustView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        initView(attrs);
    }

    SeekBar seekbar_circle_fillter, seekbar_circle_size;
    ImageView iv_shape_style;

    Count_DetailInfo countDetailInfo;

    public void setCountDetailInfo(Count_DetailInfo countDetailInfo) {
        this.countDetailInfo = countDetailInfo;

        if (countDetailInfo != null) {
            //初始化圆点直径进度
            seekbar_circle_size.setProgress(ImageRecConfig.getCircleSize(getContext()) - 3);

            //初始化精度进度
            seekbar_circle_fillter.setMax(countDetailInfo.radius_max - countDetailInfo.radius_min);
            seekbar_circle_fillter.setProgress(countDetailInfo.mostRadius - countDetailInfo.radius_min);

            Log.i("countDetailInfo.radius_min:"+ countDetailInfo.radius_min + " countDetailInfo.radius_max:"+ countDetailInfo.radius_max);

            seekbar_circle_size.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
                @Override
                public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                    //改变圆的大小
//                double radius_scale = 0;
//                if (progress > 5) {
//                    radius_scale = (progress - 5) * 2 + 10;
//                } else {
//                    radius_scale = progress + 5;
//                }
//                countDetailInfo.radius_scale = radius_scale / 10d;
//                Log.i("countDetailInfo.radius_scale:" + countDetailInfo.radius_scale);
                    ImageRecConfig.setCircleSize(getContext(), progress + 3);
                    EventBus.getDefault().post(new Event_Count_ChangeCircleRadius());
                }

                @Override
                public void onStartTrackingTouch(SeekBar seekBar) {

                }

                @Override
                public void onStopTrackingTouch(SeekBar seekBar) {

                }
            });
            seekbar_circle_fillter.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
                @Override
                public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                    //改变精度的大小
                    if(fromUser) {
                        countDetailInfo.mostRadius = progress + countDetailInfo.radius_min;
                        EventBus.getDefault().post(new Event_Count_ChangeCircleRadius());
                    }
                }

                @Override
                public void onStartTrackingTouch(SeekBar seekBar) {

                }

                @Override
                public void onStopTrackingTouch(SeekBar seekBar) {

                }
            });
        }

    }

    private void initView(AttributeSet attrs) {
        View.inflate(context, R.layout.view_commcount_adjust, this);
        seekbar_circle_fillter = findViewById(R.id.seekbar_circle_fillter);
        seekbar_circle_size = findViewById(R.id.seekbar_circle_size);
        iv_shape_style = findViewById(R.id.iv_shape_style);
        iv_shape_style.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.iv_shape_style) {
            int style = ImageRecConfig.getCircleStyle(context);
            if (style == 0) {
                ImageRecConfig.setCircleStyle(context, 1);
            } else {
                ImageRecConfig.setCircleStyle(context, 0);
            }
            checkStyle();
            EventBus.getDefault().post(new Event_Count_ChangeCircleRadius());
        }
    }

    //切换形状
    private void checkStyle() {
        if (ImageRecConfig.getCircleStyle(context) == 0) {
            //圆形
            iv_shape_style.setImageResource(R.drawable.ico_count_editsize_shape_circle);
        } else {
            //加号
            iv_shape_style.setImageResource(R.drawable.ico_count_editsize_shape_jiahao);
        }
    }
}
